<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" size="mini" @click="rebootConsoleHandler"
          >重启控制台</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <router-link to="/conf/system">
          <el-button type="primary" size="mini" @click="handleConf"
            >系统配置</el-button
          >
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="getMasterList"
        ></el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="masters">
      <el-table-column label="管理进程编号" prop="index"> </el-table-column>
      <el-table-column label="管理进程主机" prop="host" align="center" min-width="150"/>
      <el-table-column label="内部端口" prop="innerPort" align="center" />
      <el-table-column label="外部端口" prop="outerPort" align="center" />
      <el-table-column label="是否在线" align="center" min-width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            v-show="scope.row.online"
            style="color: limegreen"
            >在线</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            v-show="!scope.row.online"
            disabled
            >不在线</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="上线时间" prop="uptime" align="center" min-width="150"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="250"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="startHandler(scope.row)"
            >启动管理进程</el-button
          >
          <el-button size="mini" type="text" @click="stopHandler(scope.row)"
            >停止管理进程</el-button
          >
          <el-button size="mini" type="text" @click="logHandler(scope.row)"
            >日志</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import {
  getMasterList,
  startMaster,
  stopMaster,
  rebootConsole,
} from "@/api/console";
export default {
  name: "master",
  data() {
    return {
      loading: false,
      masters: [],
    };
  },
  created() {
    this.getMasterList();
  },
  methods: {
    getMasterList() {
      this.loading = true;
      getMasterList()
        .then((res) => {
          this.masters = res.data;
          this.loading = false;
        })
        .catch((msg) => {
          this.loading = false;
        });
    },
    startHandler(row) {
      this.$confirm("是否确认启动管理进程编号" + row.index + "?", "确定", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return startMaster(row.index);
        })
        .then((res) => {
          this.msgSuccess("操作成功，请刷新");
          getMasterList();
        })
        .catch((msg) => {
          console.log(msg);
        });
    },
    stopHandler(row) {
      this.$confirm("是否确认停止管理进程编号" + row.index + "?", "确定", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return stopMaster(row.index);
        })
        .then((res) => {
          this.msgSuccess("操作成功，请刷新");
          getMasterList();
        })
        .catch((msg) => {
          console.log(msg);
        });
    },
    logHandler(row) {
      const dir = "/master";
      const path = row.homePath + "/logs/master/master-" + row.index + ".log";
      this.$store.commit("log/SET_HOST", row.host);
      this.$store.commit("log/SET_PATH", path);
      this.$store.commit("log/SET_DIR", dir);
      this.$router.push("/service/log");
    },
    rebootConsoleHandler() {
      this.$confirm("是否确认重启控制台?", "确定", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return rebootConsole();
        })
        .then((res) => {
          this.msgSuccess("操作成功，请刷新");
          getMasterList();
        })
        .catch((msg) => {
          console.log(msg);
        });
    },
    handleConf() {},
  },
};
</script>

